package com.gxa.service.Impl;

import com.gxa.common.ResponseData;
import com.gxa.mapper.ActivityMapper;
import com.gxa.pojo.Activity;
import com.gxa.pojo.Dept;
import com.gxa.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService{
    @Autowired(required = false)
    private ActivityMapper activityMapper;
    // 查询活动信息
    @Override
    public ResponseData queryActivityList(Integer currentPage, Integer pageSize) {
        ResponseData rd = new ResponseData();
        Integer totalSize = activityMapper.countActivity();
        rd.setCount(totalSize);
        Integer params1 = (currentPage - 1)*pageSize;
        Integer params2 = pageSize;
        List<Activity> activityList = activityMapper.queryActivityList(params1,params2);
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(activityList);
        return rd;
    }
    // 添加活动
    @Override
    public ResponseData addActivity(Activity activity) {
        ResponseData rd = new ResponseData();
        int result = activityMapper.addActivity(activity);
        if(result > 0){
            rd.setCode(200);
            rd.setMsg("添加活动成功");
        }
        return rd;
    }
    // 修改活动信息
    @Override
    public ResponseData updateActivity(Activity activity) {
        ResponseData rd = new ResponseData();
        int result = activityMapper.updateActivity(activity);
        if(result > 0){
            rd.setCode(200);
            rd.setMsg("修改活动信息成功");
        }
        return rd;
    }
    //根据活动id查询活动信息
    @Override
    public ResponseData editActivity(Integer activity_id) {
        ResponseData rd = new ResponseData();
        Activity activity = activityMapper.editActivity(activity_id);
        if(activity != null){
            rd.setCode(200);
            rd.setMsg("操作成功");
            rd.setData(activity);
        }
        return rd;
    }
    //删除活动
    @Override
    public ResponseData delActivity(Integer activity_id) {
        ResponseData rd = new ResponseData();
        int result = activityMapper.delActivity(activity_id);
        if(result > 0) {
            rd.setCode(200);
            rd.setMsg("删除活动成功");
        }
        return rd;
    }
    //关键字搜索活动信息
    @Override
    public ResponseData searchActivityList(Integer currentPage, Integer pageSize, String keyword) {
        ResponseData rd = new ResponseData();
        Integer totalSize = activityMapper.countSearchActivityList(keyword);
        rd.setCount(totalSize);
        Integer params1 = (currentPage - 1)*pageSize;
        Integer params2 = pageSize;
        if(keyword == null || "".equals(keyword)){
            List<Activity> activityList = activityMapper.queryActivityList(params1,params2);
            rd.setCode(200);
            rd.setMsg("操作成功");
            rd.setData(activityList);
            return rd;
        }
        List<Activity> activityList = activityMapper.searchActivityList(params1,params2,keyword);
        rd.setCode(200);
        rd.setMsg("操作成功");
        rd.setData(activityList);
        return rd;
    }
    // 将图片的相对路径保存到数据库
    @Transactional
    @Override
    public void uploadFile(String fileDataBasePath) {
        activityMapper.uploadFile(fileDataBasePath);
    }
    //修改活动没有修改图片
    @Transactional
    @Override
    public ResponseData updateActivityNoFile(Activity activity) {
        ResponseData rd = new ResponseData();
        int result = activityMapper.updateActivityNoFile(activity);
        if(result > 0){
            rd.setCode(200);
            rd.setMsg("修改活动信息成功");
        }
        return rd;
    }
}
